#include "widget.h"
#include "ui_widget.h"
#include"QTableWidgetItem"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->tableWidget->insertRow(0);//指定位置添加行
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);
    //设置列名
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("姓名"));//setHorizontalHeaderItem()方法会接管传入的QTableWidgetItem的所有权
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("学号"));//表格销毁时会自动销毁所有关联的header items
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("性别"));//QTableWidget的API设计明确要求传入指针
    ui->tableWidget->setItem(0,0,new QTableWidgetItem("张三"));
    ui->tableWidget->setItem(1,0,new QTableWidgetItem("李四"));
    ui->tableWidget->setItem(2,0,new QTableWidgetItem("王五"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("11"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("22"));
    ui->tableWidget->setItem(2,1,new QTableWidgetItem("33"));
    ui->tableWidget->setFont(QFont("楷体"));





}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_addrow_clicked()
{
    int row=ui->tableWidget->currentRow();
    ui->tableWidget->insertRow(row+1);//在选中行的下一行添加

}

void Widget::on_pushButton_delrow_clicked()
{
    int row=ui->tableWidget->currentRow();
    ui->tableWidget->removeRow(row);
}

void Widget::on_pushButton_addcu_clicked()
{
    int column=ui->tableWidget->currentColumn();
    ui->tableWidget->insertColumn(column+1);//在选中列的右边添加
    QString name=ui->lineEdit->text();
    ui->tableWidget->setHorizontalHeaderItem(column+1,new  QTableWidgetItem(name));
}

void Widget::on_pushButton_delcu_clicked()
{
    int column=ui->tableWidget->currentColumn();
    ui->tableWidget->removeColumn(column);
}
